#define _CRT_SECURE_NO_WARNINGS 1
#include "newQueue.h"

void Init(Q* qq)
{
	assert(qq);
	qq->head = NULL;
	qq->tail = NULL;
	qq->size = 0;
}

void QueuePush(Q* qq, DataType x)
{
	assert(qq);

	Queue* temp = (Queue*)malloc(sizeof(Queue));
	if (temp == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	temp->data = x;
	temp->next = NULL;

	if (qq->tail == NULL)
		qq->head = qq->tail = temp;
	else
	{
		qq->tail->next = temp;
		qq->tail = temp;
	}
		
	qq->size++;
}

void QueuePop(Q* qq)
{
	assert(qq);
	assert(!Empty(qq));

	if (qq->head == qq->tail)
	{
		free(qq->head);
		qq->head = qq->tail = NULL;
	}
	else
	{
		Queue* next = qq->head->next;
		free(qq->head);
		qq->head = next;
	}

	qq->size--;
}

DataType GetQueueFrontNum(Q* qq)
{
	assert(qq);
	assert(!Empty(qq));

	return qq->head->data;
}

DataType GetQueueBackNum(Q* qq)
{
	assert(qq);
	assert(!Empty(qq));

	return qq->tail->data;
}

bool Empty(Q* qq)
{
	assert(qq);

	return qq->size == 0;
}

void Destroy(Q* qq)
{
	assert(qq);

	free(qq->head);
	free(qq->tail);
	free(qq);
}

int Size(Q* qq)
{
	assert(qq);

	return qq->size;
}